package in.muton.edushare.pages.user;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.user.panels.MyBannerPanel;
import in.muton.edushare.pages.user.panels.MyLeftMenuPanel;
import in.muton.edushare.pages.user.panels.YourBannerPanel;
import in.muton.edushare.pages.user.panels.YourLeftMenuPanel;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.RestartResponseException;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Users;

public abstract class UserBase extends EduShareBasePage {

	public UserBase(PageParameters params) {
		getUser(params);
		ud = new UserDataHandler(getModel());
		if(getModel().getObject().getObjectId().equals(getLoggedInUser().getObjectId())){
			add(new MyBannerPanel("ubanner", this));
			add(new MyLeftMenuPanel("lmenu", this));
		}else{
			add(new YourBannerPanel("ubanner", this));
			add(new YourLeftMenuPanel("lmenu", this));
		}
		
	}

	private void getUser(PageParameters params) {
		
		String id = (params!=null)?params.getString("0", ""):null;
		Users user = null;
			
		//if someone entered wrong user name show 404
		if (StringUtil.isValid(id)) {
			user = new UsersDAO(getSessionORMInterface()).getUserFromUserName(id);
			if(user==null){
				throw new RestartResponseException(URLLoader.getPageClass(URLConstants.PAGE_NOT_FOUND_PATH));
			}
		}else{
			model = new CayenneModel<Users>(EduShareSession.get().getLoggedInUser());
			return;
		}
		model = new CayenneModel<Users>(user);
	}

	private IModel<Users> model;

	public IModel<Users> getModel() {
		return model;
	}

	public void setModel(IModel<Users> model) {
		this.model = model;
	}

	/** List of Active pages can be found on the left panels */
	public abstract String getActivePage();

	protected UserDataHandler ud = null;
}
